package com.li.spark.share

import org.apache.spark.{SparkConf, SparkContext}

/**
 * 广播变量（readonly）
 */
object BroadcastOp {
  def main(args: Array[String]): Unit = {
    val sc: SparkContext = getSparkContext

    val RDD = sc.parallelize(Array(1, 2, 3, 4, 5), 2)
    val varable = 2
    //定义广播变量
    var varableBroadcast = sc.broadcast(varable)

    //使用广播变量，调用value方法
    RDD.map(_ * varableBroadcast.value).foreach(println(_))

    sc.stop();
  }


  private def getSparkContext = {
    //创建SparkContext
    val conf = new SparkConf();
    conf.setAppName("BroadcastOp").setMaster("local");
    new SparkContext(conf);
  }
}
